//index.js
//获取应用实例

/* 其他页面跳转到此详情页面需要带的参数 id, type, ouid */
var app = getApp();
var constract = require('../../utils/constract.js');
var comm = require('../../utils/common.js');

//id(cardId) type(扫描or用户) ouid(他人用户 id) h(截图的高度自动获取)
var id, type=1, ouid, h=620; 

function showOther(token,type,callback){
  wx.request({
    url: constract.other, //他人详情
    header: { token: token },
    data: {passiveUid: ouid, type: type, cardId: id},
    success: callback,
    fail: (err)=>{
      console.log(err);
    }
  })
}


Page({
  data: {
    userInfo:[],
    erm: false,
    moreMsg: false, //点击加载更多信息
    label:'',
    imgList: [],
    interval: 3000,
    duration: 500,
  },

  onLoad: function (options) {
    console.log(options);
    
    var that = this;
    
    id = options.id; //id 为 card id
    ouid = options.ouid; //uid 为 别人用户id
    type = options.type;

    var collectMe = options.collectMe; 

    that.setData({
      type: type,
      ouid: ouid,
      sourceList: app.globalData.sourceList, 
    })
    
    if(collectMe){
      that.setData({
        collectMe: collectMe
      })
    }

  },

  onShow:function(){
    var that=this;
    
    app.Verification(function (token) {
      
      comm.raisePopilarity(token,id); //人气增加
      
      /*查看他人详情*/
      showOther(token,type,function (res) {
        
        console.log(res);

        var imgList = [];
        var cardImg = res.data.data.cardImg;
        var backImg = res.data.data.backImg;
        cardImg && imgList.push(cardImg);
        backImg && imgList.push(backImg);

        if (res.data.data.state === 0) {
          wx.showModal({
            title: '提示',
            content: '对方还没有创建名片！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
          return;
        }

        // console.log(res.data.data);
        // console.log('------------------');

        if(res.data.data.tel_cell) {
          var param = res.data.data.tel_cell.length;
          h = 620+param*140;
        }

        that.setData({
          userInfo: res.data.data,
          imgList: imgList,
          h: h,
          source: res.data.data.source,
        })

        console.log(that.data);

      })

    })
  },
  // onHide: function() {
  //   var imgList = this.data.imgList;
  //   imgList = [];
  //   this.setData({
  //     imgList: imgList
  //   })
  // },
  //保存到通讯录
  phoneNumTap: function () {
    var that = this;
    var mobilePhoneNumber,workPhoneNumber,hostNumber,homePhoneNumber,title,organization,
        weChatNumber,email,workAddressStreet,url,workFaxNumber;


    if (that.data.userInfo.tel_cell){
      mobilePhoneNumber = that.data.userInfo.tel_cell[0];
      workPhoneNumber = that.data.userInfo.tel_cell[1];
      homePhoneNumber = that.data.userInfo.tel_cell[2];
    }
    if (that.data.userInfo.tel_work) {
      hostNumber = that.data.userInfo.tel_work[0];
    }
    if (that.data.userInfo.phone){
      mobilePhoneNumber = that.data.userInfo.phone;
    }
    
    if(that.data.userInfo.title){
      title = that.data.userInfo.title.join(',');
    }
    if(that.data.userInfo.job_title){
      title = that.data.userInfo.job_title;
    }

    if(that.data.userInfo.company){
      organization = that.data.userInfo.company.join(',');
    }
    if(that.data.userInfo.company_name){
      organization = that.data.userInfo.company_name;
    }

    if(that.data.userInfo.wechat){
      weChatNumber = that.data.userInfo.wechat.join(',');
    }

    if(that.data.userInfo.email && Array.isArray(that.data.userInfo.email)){ //需要判断是不是数组
      email = that.data.userInfo.email.join(',');
    }else if(that.data.userInfo.email) {
      email = that.data.userInfo.email;
    }

    if(that.data.userInfo.addr){
      workAddressStreet = that.data.userInfo.addr.join(',');
    }
    if(that.data.userInfo.web_page){
      url = that.data.userInfo.web_page.join(',');
    }
    if(that.data.userInfo.fax){
      workFaxNumber = that.data.userInfo.fax.join(',');
    }
    
    
    wx.addPhoneContact({
      firstName: that.data.userInfo.name,
      mobilePhoneNumber: mobilePhoneNumber,//联系人手机号  
      workPhoneNumber: workPhoneNumber, //工作电话
      hostNumber: hostNumber, //公司电话
      homePhoneNumber: homePhoneNumber, //住宅电话
      title: title,
      organization:organization,
      weChatNumber:weChatNumber,
      email:email,
      workAddressStreet:workAddressStreet,
      url:url,
      workFaxNumber:workFaxNumber,
      success:()=>{},
      fail:()=>{},
      complete:()=>{}
    })

  },
  praised:function(){
    var that = this;
    var ouid = this.data.userInfo.uid;
    var cardId = this.data.userInfo.id;
    var likeState = this.data.userInfo.likeState;
    app.Verification(function (token) {
      wx.request({
        url: constract.likeAddOrEdit, //********************取消点赞和重新点赞
        header: { token: token },
        data: { like_uid: ouid, type: 1,cardId: cardId },
        success: function (res) {
          console.log(res);

          showOther(token, type, function (res) {
            if (likeState!=0){
              that.setData({
                userInfo: res.data.data,
                flag: true
              })
            }else {
              that.setData({
                userInfo: res.data.data
              })
            }         
          })

        }
      })
    })
  },
  // collected: function () {
  //   var that = this;
  //   var id = this.data.userInfo.id;
  //   var collectState = this.data.userInfo.collectState;

  //   app.Verification(function (token) {
  //     wx.request({
  //       url: constract.collectionAddOrEdit, // *****************取消收藏和重新收藏接口
  //       header: { token: token },
  //       data: { collection_uid: id, is_delete:collectState },
  //       success: function (res) {
          
  //         showOther(token,function (res) {
  //           if (collectState != 0) {
  //             that.setData({
  //               userInfo: res.data.data,
  //               sep: true
  //             })
  //           } else {
  //             that.setData({
  //               userInfo: res.data.data
  //             })
  //           } 
            
  //         })

  //       }
  //     })
  //   })
  // },
  previewImg:function(e){
    var urls = [];

    var src = e.currentTarget.dataset.src;

    var cardImg = this.data.userInfo.cardImg;
    var backImg = this.data.userInfo.backImg;
    var avatar = this.data.userInfo.avatar;
    
    var uploadType = this.data.userInfo.upload;

    if(uploadType == 2) {
      // cardImg += '?x-oss-process=image/crop,y_200,h_'+h;
      cardImg && urls.push(cardImg);
    } else{
      cardImg && urls.push(cardImg);
    }

    backImg && urls.push(backImg);

    avatar ? urls.push(avatar) : urls.push('https://pic.51fen.com/webadmin/2018081515120801518.png'); 

    wx.previewImage({
      current: src,
      urls:urls,
    })
  },
  moreMsg: function() {
    if(this.data.moreMsg) {
      this.setData({
        moreMsg: false
      })
    } else {
      this.setData({
        moreMsg: true
      })
    }
  },
  //取消收藏
  cancelCollection: function(){
    console.log(this.data);
    var id = this.data.userInfo.id;
    var ouid = this.data.userInfo.uid;
    var collectState = this.data.userInfo.collectState;

    var msg;
    collectState == 1 ? msg='确认收藏该名片?' : msg='确认对此名片取消收藏?'


    wx.showModal({
      title: "提示",
      content: msg,
      success: function(res){
        if(res.confirm) {
          app.Verification(function (token) {
            wx.request({
              url: constract.collectionAddOrEdit,
              header: {token: token},
              data: { collection_uid: ouid, is_delete:collectState, cardId: id },
              success: function (res) {
                if (res.data.code) {
                  wx.switchTab({
                    url: '../card/card',
                  })
                }
              }
            })
          })
        }
      }
    })
  },
  //删除名片
  delete: function() {
    // var id = this.data.id;
    wx.showModal({
      title: "提示",
      content: "确认删除这个名片?",
      success: function(res){
        if(res.confirm) {
          wx.request({
            url: constract.cardDelete,
            data: { id: id },
            success: function (res) {
              if (res.data.code) {
                wx.switchTab({
                  url: '../card/card',
                })
              }
            }
          })
        }
      }
    })
  },
  //编辑名片
  edit: function() {
    // var id = this.data.id;
    var msg = JSON.stringify(this.data.userInfo);
    // console.log(msg);
    wx.redirectTo({
      url:'../input-card/input-card?editId='+id+'&msg='+msg+'&uploadType='+this.data.userInfo.upload,
    })
  },

  
  choseSource:function(){ //调出弹框
    this.setData({
      showSource: true
    })
  },
  hideSources: function(){ //未选隐藏
    this.setData({
      showSource: false
    })
  },
  hasSource: function(e){ //选择隐藏
    var that = this;
    var source = e.currentTarget.dataset.source;
    var cardId = this.data.userInfo.id;
    var ouid = this.data.ouid;
    var type = this.data.type;

    var userInfo  = this.data.userInfo;

    // console.log(this.data);

    if(source) {
      app.Verification(function (token) {
        wx.request({
          url: constract.editSource,
          header: {token: token},
          data: {cardId: cardId, collection_uid: ouid, source: source,type: type},
          success: (res) => {
            // console.log(res);
            if(res.data.code == 12000) {
              that.setData({
                source: source,
              })
            }
          }
        })
      })
    }
    this.setData({
      showSource: false
    })
  },
})